package System;
import javax.swing.JOptionPane;

import client.ChatClient;

import common.ChatIF;

/**
 * This class constructs the UI for a chat client.  It implements the
 * chat interface in order to activate the display() method.
 * Warning: Some of the code here is cloned in ServerConsole 
 *
 * @author Baruch Cohen
 * @author Nir Bazak
 * @author Gil klebanov
 * @author Ivgeny
 * @version June 2012
 * 
 */
public class ClientConsole implements ChatIF 
{
  //**Class variables**//
   /**
   * The default port to connect on.
   * The default host.
   */
  final public static int DEFAULT_PORT = 5555;
  final public static String DEDAULT_HOST = "localhost";
  
  //Instance variables **********************************************
   /**
   * The instance of the client that created this ConsoleChat.
   */
  ChatClient client;

  
  //Constructors ****************************************************
  /**
   * Constructs an instance of the ClientConsole UI.
   *
   * @param host The host to connect to.
   * @param port The port to connect on.
   */
  public ClientConsole(String host, int port) throws Exception
  {
      client= new ChatClient(host, port, this);
  }

  
  //Instance methods ************************************************


  /**
   * This method overrides the method in the ChatIF interface.
   * It displays a message onto the screen.
   *
   * @param message The string to be displayed.
   */
  public void display(String message) 
  {
    JOptionPane.showMessageDialog(null, message, "System Message", JOptionPane.INFORMATION_MESSAGE);
  }
  
  public void terminateConnection() throws Exception
  {
	  client.closeConnection();
  }
  
  public void renewConnection() throws Exception
  {
	  client.openConnection();
  }
  
  public void send(Object msg)
  {
	 client.handleMessageFromClientUI(msg);
  }
  
  public Object getResult()
  {
	  return(client.getResult());   
  }
   
 //**Class methods**//
}